package com.glory.wxelectricitypda.utils;

import android.util.Log;

import com.glory.wxelectricitypda.base.Constant;
import com.glory.wxelectricitypda.bean.AuthorityEntity;
import com.glory.wxelectricitypda.bean.BarCodeCheckBean;
import com.glory.wxelectricitypda.bean.BaseResponseBean;
import com.glory.wxelectricitypda.bean.BoxPackageTypeEntity;
import com.glory.wxelectricitypda.bean.EqpEntoty;
import com.glory.wxelectricitypda.bean.LoginBean;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.QcCheckListBean;
import com.glory.wxelectricitypda.bean.RequestHeaderBean;
import com.glory.wxelectricitypda.bean.ResponseAfterPackBean;
import com.glory.wxelectricitypda.bean.ResponseQueryBean;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.PostRequest;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_VersionEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_NetUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hx
 * @date 2019/6/17
 * @desc
 */
public class NetUtils {
    public static void requestWithNormalCallBack(String method, JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<?>> callBack) {
        FreeApi_NetUtils.requestGetNet("http://192.168.18.50:18080/esbrest/RestService/sendrequest", method, getRequestMap(map, method), callBack);
    }

    public static Map<String, String> getRequestMap(JSONObject bodyMap, String method) {
        Map<String, String> map = new HashMap<>();
        map.put("requestMessage", getRequestJson(new RequestHeaderBean(method, Constant.CURRENT_USER), bodyMap));
        map.put("senderId", "PDA2MESSender");
        map.put("timeOut", "5000");
        FreeApi_LogUtils.saveErrorMessages(map.toString(), "请求REQUEST");
        return map;
    }

    public static Map<String, String> getRequestMap(JSONObject bodyMap, String method, String language) {
        Map<String, String> map = new HashMap<>();
        map.put("requestMessage", getRequestJson(new RequestHeaderBean(method, Constant.CURRENT_USER,language), bodyMap));
        map.put("senderId", "PDA2MESSender");
        map.put("timeOut", "5000");
        FreeApi_LogUtils.saveErrorMessages(map.toString(), "请求REQUEST");
        return map;
    }

    public static Map<String, String> getRequestJson(JSONObject bodyMap, String method) {
        Map<String, String> map = new HashMap<>();
        map.put("requestMessage", getRequestJson(new RequestHeaderBean(method, Constant.CURRENT_USER), bodyMap));
        map.put("senderId", "PDA2MESSender");
        map.put("timeOut", "5000");
        FreeApi_LogUtils.saveErrorMessages(map.toString(), "请求REQUEST");
        return map;
    }

    public static String getRequestJson(RequestHeaderBean headerEntity, JSONObject bodyMap) {
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject1.put("Header", new JSONObject(new Gson().toJson(headerEntity)));
            jsonObject1.put("Body", bodyMap);
            jsonObject.put("Request", jsonObject1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }

    public static JSONObject getRequestJson(String messageName, JSONObject bodyMap) {
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject1.put("Header", new JSONObject(new Gson().toJson(
                    new RequestHeaderBean(messageName, Constant.CURRENT_USER))));
            jsonObject1.put("Body", bodyMap);
            jsonObject.put("Request", jsonObject1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    public static JSONObject getRequestJsonObject(String messageName, JsonObject bodyMap) {
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject1.put("Header", new JSONObject(new Gson().toJson(
                    new RequestHeaderBean(messageName, Constant.CURRENT_USER))));
            jsonObject1.put("Body", bodyMap);
            jsonObject.put("Request", jsonObject1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    public static void requestScrap(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<ResponseQueryBean>> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITSCRAPCODE", getRequestMap(map, "EUIT.EUITSCRAPCODE"), callBack);
    }


    public static void requestLogin(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<LoginBean>> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITLOGIN", getRequestMap(map, "EUIT.EUITLOGIN","EN"), callBack);
    }

    public static void requestQueryLot(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<ResponseQueryBean>> callBack) {
        FreeApi_LogUtils.saveErrorMessages(map.toString(), "请求参数");
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITLOTQUERY", getRequestMap(map, "EUIT.EUITLOTQUERY"), callBack);
    }
    public static void requestQueryLot2(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<QcCheckListBean>> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITLOTQUERY", getRequestMap(map, "EUIT.EUITLOTQUERY"), callBack);
    }
    public static void requestUpdate(FreeUI_EntityCallBack<FreeUI_VersionEntity> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.FILE_URL, "requestUpdate", null, callBack);
    }

    public static void requestPack(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<ResponseAfterPackBean>> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITPACKING", getRequestMap(map, "EUIT.EUITPACKING"), callBack);
    }

    public static void requestBarCodeCheck(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<BarCodeCheckBean>> callBack) {
        FreeApi_LogUtils.saveErrorMessages(map.toString(), "请求参数");
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITPRINTCHECK", getRequestMap(map, "EUIT.EUITPRINTCHECK"), callBack);
    }

    public static void requestAuthority(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<AuthorityEntity>> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITAUTHORITY", getRequestMap(map, "EUIT.EUITAUTHORITY"), callBack);
    }

    public static void requestBoxPackageTypeQuery(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<BoxPackageTypeEntity>> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITPACKINGCHECK", getRequestMap(map, "EUIT.EUITPACKINGCHECK"), callBack);
    }

    public static void requestEqpInfo(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<EqpEntoty>> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "getEqpInfo", getRequestMap(map, "GetEqpInfo"), callBack);
    }

    public static void requestEuitkjtsQuery(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<LotBean>> callBack) {
        FreeApi_NetUtils.requestGetNet(Constant.REQUEST_URL, "EUIT.EUITKJTSQUERY", getRequestMap(map, "EUIT.EUITKJTSQUERY"), callBack);
    }

    public static void requestLotMHShip(JSONObject map, FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack) {
        Log.e("请求参数",getRequestMap(map, "EUIT.LOTMHSHIP").toString());
        FreeApi_NetUtils.requestPostNet(Constant.REQUEST_URL, "EUIT.LOTMHSHIP", getRequestMap(map, "EUIT.LOTMHSHIP"), callBack);
    }

    public static <T> void requestPostUtils(String url,String messageName,JSONObject map,Object tag, FreeUI_EntityCallBack<T> callBack) {
        FreeApi_LogUtils.saveErrorMessages(map.toString(), "请求参数");
        (((PostRequest) OkGo.post(Constant.REQUEST_URL+"/api/euit/pda/"+url).tag(tag)).upJson(getRequestJson(messageName, map))).execute(callBack);
    }
    public static <T> void requestEAPPostUtils(String url,String messageName,JSONObject map,Object tag, FreeUI_EntityCallBack<T> callBack) {
        FreeApi_LogUtils.saveErrorMessages(map.toString(), "请求参数");
        (((PostRequest) OkGo.post(Constant.REQUEST_URL+"/EIB/EAP/"+url).tag(tag)).upJson(getRequestJson(messageName, map))).execute(callBack);
    }
    //通用接口
    public static <T> void requestPostMesForwardUtils(JSONObject map,Object tag, FreeUI_EntityCallBack<T> callBack) {
        FreeApi_LogUtils.saveErrorMessages(map.toString(), "请求参数");
        (((PostRequest) OkGo.post(Constant.REQUEST_URL+"/api/euit/pda/mesForward").tag(tag)).upJson(getRequestJson("MesForward", map))).execute(callBack);
    }

}
